<?php

declare(strict_types=1);

namespace app\admin\model;

use think\Model;
use think\model\concern\SoftDelete;

/**
 * @mixin \think\Model
 */
class User extends Model
{
    //软删除
    use SoftDelete;
    // 设置字段信息
    protected $pk = 'id';
    protected $convertNameToCamel = true;
    protected $schema = [
        'id' => 'bigint',
        'user_id' => 'char',
        'avatar' => 'varchar',
        'user_name' => 'varchar',
        'nickname' => 'varchar',
        'phone_number' => 'varchar',
        'sex' => 'char',
        'user_password' => 'varchar',
        'status' => 'char',
        'login_ip' => 'varchar',
        'login_address' => 'varchar',
        'login_time' => 'datetime',
        'create_time' => 'datetime',
        'create_by' => 'varchar',
        'update_time' => 'datetime',
        'update_by' => 'varchar',
        'delete_time' => 'datetime'
    ];
    protected $deleteTime = 'delete_time';
    //关联角色
    public function role()
    {
        return $this->belongsToMany(Role::class, 'user_role');
    }


    public function setAvatarAttr($value)
    {
        $domain = request()->domain();
        return $value ? str_replace($domain, '', $value) : '';
    }

    public function getAvatarAttr($value)
    {
        $domain = request()->domain();
        return $value ? $domain . $value : '';
    }
}
